﻿var actionURL = "ashx/PIC.ashx";
var uid;
var cid = "";
var hDialog;
var pSize = 3;
var count = 0;
var filterObj;
var picname = "";
var bpid = "";
var datagridpic;
function renderinfopic(getdata) {
    var html = template('tmppic', getdata);
    html = html + "<div style='clear: both;'></div>";
    $('#infos').html(html);
    $(".small_pic li").click(function () {
        $(this).siblings().removeClass("on");
        $(this).addClass("on");
        var preNumber = $(this).prevAll().size() + 1;
        $(".big_pic li").removeClass("on");
        $(".big_pic li:nth-child(" + preNumber + ")").addClass("on");
        picname = $(this).find("img").attr("alt");
        bpid = picname.split('_')[0];
        InitGird();
    });
    $(".small_pic li:eq(0)").addClass("on");
    $(".big_pic li:eq(0)").addClass("on");
    var elements = document.querySelectorAll('.demo-image');
    Intense(elements);
}
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
var mylayout = {
    init: function (size) {
        $('#layout').width(size.width - 4).height(size.height - 4).layout();
        var center = $('#layout').layout('panel', 'center');
        center.panel({
            onResize: function (w, h) {
                //$('#histList').datagrid('resize', { width: w, height: h });
            }
        });
    },
    resize: function (size) {
        mylayout.init(size);
        $('#layout').layout('resize');
    }
};

$(function () {
    var size = { width: $(window).width(), height: $(window).height() };
    mylayout.init(size);
    uid = getUrlParam("uid");
    $('#dtOpstart,#dtOpend').datebox({ width: 100 });
    DicCategory.bindTree();
    //autoResize({ dataGrid: '#histList', gridType: 'datagrid', callback: grid.bind, height: 0 });
    $('#a_search').click(search);
    //$('#a_send').click(CRUD.show);
    $("#yjfx").panel({
        href: '../html/alarmqzz.html',
        title: '孢子预警',
        tools: [{ iconCls: 'icon-phone', handler: function () { sendAlarm('孢子预警'); } }],
        onLoad: function () {
            InitSendUser();
            $("#btnyj").click(function () {
                sendAlarm('孢子预警');
            });
        }

    });
});

var DicCategory = {
    bindTree: function () {
        $('#cdbNum').tree({
            url: '../devinfo/GetDev.ashx?Action=queryuid&devtype=BZP&uid=' + uid,
            onLoadSuccess: function (node, data) {
                if (data.length == 0) {
                    $('#noCategoryInfo').fadeIn();
                }
                $('body').data('categoryData', data);
            },
            onClick: function (node) {
                var cc = node.id;
                cid = cc;
                //$.ajax({
                //    type: "post",
                //    dataType: "json",
                //    url: "../devinfo/GetDev.ashx?action=querycbd1&devid=" + cid,
                //    success: function aa(data) {
                //        //ybqid = data[0].cbdid;
                        
                //    }
                //});
                StartTime = $("#StartTime").datebox("getValue");
                EndTime = $("#EndTime").datebox("getValue");
                $.ajax({
                    type: "post",
                    dataType: "json",
                    //url: actionURL + "?action=detail&rows=" + pSize + "&page=1&devID=" + cid + "&StartTime=" + StartTime + "&EndTime=" + EndTime,
                    url: "GetPic.ashx?action=query&rows=" + pSize + "&page=1&devID=" + cid + "&StartTime=" + StartTime + "&EndTime=" + EndTime,
                    success: function aa(data) {
                        count = data.total;
                        renderinfopic(data);
                        $('#pp').pagination({
                            pageSize: pSize,
                            total: count,
                            pageNumber: 1,
                            pageList: [3],
                            onSelectPage: function (pageNumber, pageSize) {
                                StartTime = $("#StartTime").datebox("getValue");
                                EndTime = $("#EndTime").datebox("getValue");
                                $.ajax({
                                    type: "post",
                                    dataType: "json",
                                    url: "GetPic.ashx?action=query&rows=" + pageSize + "&page=" + pageNumber + "&devID=" + cid + "&StartTime=" + StartTime + "&EndTime=" + EndTime,
                                    success: function aa(data) {
                                        renderinfopic(data);
                                    }
                                });
                            }
                        });
                    }
                });
                InitGridhw();
            }
        });
    },
    reload: function () {
        $('#cdbNum').tree('reload');
    },
    getSelected: function () {
        return $('#cdbNum').tree('getSelected');
    }
};
function InitGird() {
    $('#ptstr').val(picname);
    $('#zddate').datagrid({
        title: picname, //表格标题
        url: 'GetPicData.ashx', //请求数据的页面
        sortName: 'ptime', //排序字段
        idField: 'pid', //标识字段,主键
        iconCls: '', //标题左边的图标
        height: '100%',
        width: '100%',
        nowrap: false, //是否换行，True 就会把数据显示在一行里
        striped: true, //True 奇偶行使用不同背景色
        collapsible: false, //可折叠
        sortOrder: 'desc', //排序类型
        remoteSort: true, //定义是否从服务器给数据排序
        fitColumns: true,
        singleSelect: true,
        method: 'post',
        columns: [[//							                    
           { field: 'ckb', checkbox: true },
            { title: '害虫', field: 'pname', width: 100 },
            { title: '数量', field: 'pnum', width: 50 }
        ]
        ],
        toolbar: "#tab_toolbars",
        queryParams: { "action": "querycz", "ptstr": picname, "ybqid": "" },
        pagination: false, //是否开启分页
        pageNumber: 1, //默认索引页
        pageSize: 5, //默认一页数据条数
        rownumbers: true,
        onLoadSuccess: function (data) {
            datagridpic = data;
        }
    });

}
function newUser() {
    $('#fm').form('clear');
    $('#dlg').dialog('open').dialog('setTitle', '添加');
    document.getElementById("test").value = "add";
    $('#ptstr').val(picname);
}
//修改管理员  
function editUser() {
    var row = $('#zddate').datagrid('getSelected');
    if (row == null) {
        $.messager.alert("提示", "请选择要修改的行！", "info");
    }
    if (row) {
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        document.getElementById("test").value = "modify";
        $('#fm').form('load', row);
    }
}
//添加        
function saveUser() {
    var test = document.getElementById("test").value;
    var f = $('#fm');
    if (test == "add") {
        f.form('submit', {
            url: "GetPicData.ashx?action=" + test,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                if (result.indexOf("T") == 0) {
                    $('#dlg').dialog('close');
                    $.messager.alert("提示", "恭喜您，信息添加成功", "info");
                    $('#zddate').datagrid('reload');
                }
                else if ((result.indexOf("F") == 0)) {
                    $.messager.alert("提示", "添加失败，请重新操作！", "info");
                    return;
                }
                else if ((result.indexOf("L") == 0)) {
                    $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                    top.location = '/index.aspx';
                    return;
                }
            }
        });

    } else {

        $('#fm').form('submit', {
            url: "GetPicData.ashx?action=" + test,
            onSubmit: function () {
                return $(this).form('validate');
            },
            success: function (result) {
                if (result.indexOf("T") == 0) {
                    $('#dlg').dialog('close');
                    $('#zddate').datagrid('clearSelections');
                    $.messager.alert("提示", "恭喜您，信息修改成功", "info");
                    $('#zddate').datagrid('reload');

                }
                else if ((result.indexOf("F") == 0)) {
                    $.messager.alert("提示", "添加失败，请重新操作！", "info");
                    return;
                }
                else if ((result.indexOf("L") == 0)) {
                    $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                    top.location = '/index.aspx';
                    return;
                }
            }
        });
    }
}
function removeUser() {
    var test = document.getElementById("test").value = "delete";
    var row = $('#zddate').datagrid('getSelected');
    if (row == null) {
        $.messager.alert("提示", "请选择要删除的行！", "info");
    }
    if (row) {
        $.messager.confirm('提示', '你确定要删除这条信息吗？', function (r) {
            if (r) {
                $('#fm').form('submit', {
                    url: 'GetPicData.ashx?pdid=' + row.pdid + "&action=" + test,
                    onSubmit: function () {
                        //return $(this).form('validate');  
                    },
                    success: function (result) {
                        if (result.indexOf("T") == 0) {
                            $('#dlg').dialog('close');
                            $('#zddate').datagrid('clearSelections'); //清空选中的行  
                            $.messager.alert("提示", "恭喜您，信息删除成功！", "info");
                            $('#zddate').datagrid('reload');
                        }
                        else if ((result.indexOf("F") == 0)) {
                            $.messager.alert("提示", "删除失败，请重新操作！", "info");
                            return;
                        }
                        else if ((result.indexOf("L") == 0)) {
                            $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                            top.location = '/index.aspx';
                            return;
                        }

                    }

                });
            }
        })
    }
}
function getid(grows) {
    var ids = "";
    for (var i = 0; i < grows.total; i++)
        ids = ids + grows.rows[i].pdid + ",";
    if (ids != "")
        ids = ids.substr(0, ids.length - 1);
    return ids;

}
function savejg() {

    if (datagridpic == null) {
        $.messager.alert("提示", "没有任何可更新的行！", "info");
    }
    if (datagridpic) {
        $.messager.confirm('提示', '你确定更新吗？', function (r) {
            if (r) {
                var ids = getid(datagridpic);
                $('#fm').form('submit', {
                    url: 'GetPicData.ashx?ids=' + ids + "&action=updateallcbd&cbdid=" + ybqid,
                    onSubmit: function () {
                        //return $(this).form('validate');  
                    },
                    success: function (result) {
                        if (result.indexOf("T") == 0) {
                            $('#dlg').dialog('close');
                            $('#zddate').datagrid('clearSelections'); //清空选中的行  
                            $.messager.alert("提示", "恭喜您，信息更新成功！", "info");
                            $('#zddate').datagrid('reload');
                        }
                        else if ((result.indexOf("F") == 0)) {
                            $.messager.alert("提示", "更新失败，没有找到相关数据！", "info");
                            return;
                        }
                        else if ((result.indexOf("L") == 0)) {
                            $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                            top.location = '/index.aspx';
                            return;
                        }

                    }

                });
            }
        })
    }
}
function InitGridhw() {
    $('#histList').datagrid({
        toolbar: '#toolbarmhw',
        url: '../weatherinfo/GetPicData.ashx?action=query',
        title: "孢子数据。",
        iconCls: 'icon icon-list',
        height:'100%',
        width: '100%',
        nowrap: true, //折行
        rownumbers: true, //行号
        striped: true, //隔行变色
        idField: 'keyid', //主键
        singleSelect: true, //单选
        fitColumns: true,
        queryParams: { bpid: bpid, "StartTime": $("#StartTime").datebox("getValue"), "EndTime": $("#EndTime").datebox("getValue") },
        columns: [[//							                    
             { title: '设备号', field: 'bpid', width: 50 },
            { title: '孢子', field: 'pname', width: 100 },
            { title: '数量', field: 'pnum', width: 50 },
            { title: '上传时间', field: 'uptime', width: 150 }
        ]
        ],
        pagination: true,
        pageSize: 10,
        onClickRow: function (index, row) {
            var pname = row.pname;
            var lvalue = 0;
            var hvalue = 0;
            var pnum = parseInt(row.pnum);
            $.ajax({
                type: "post",
                dataType: "json",
                url: "../bzinfo/GetBZ.ashx?action=queryonebz&bzname=" + pname,
                success: function aa(data) {
                    var result = "", temp = "";
                    lvalue = parseInt(data[0].lvalue);
                    hvalue = parseInt(data[0].hvalue);
                    if (pnum < lvalue) {
                        temp = "轻度";
                        $("#alarmlevel").val("一级");
                    }
                    else if (pnum < hvalue) {
                        temp = "中度";
                        $("#alarmlevel").val("二级");
                    }
                    else if (pnum > hvalue) {
                        temp = "重度";
                        $("#alarmlevel").val("三级");
                    }
                    result = row.uptime + "," + pname + ",数量为:" + pnum + ",标准为：" + lvalue + "-" + hvalue + ",结果为：" + temp;
                    $('#mmark').val(result);
                }
            });
        },
        pageList: [10, 20, 40, 50, 10000]
    });
}
function createParam(action, keyid) {
    var o = {};
    var query = top.$('#uiform').serializeArray();
    query = convertArray(query);
    o.jsonEntity = JSON.stringify(query);
    o.action = action;
    o.keyid = keyid;
    return "json=" + JSON.stringify(o);
}
function search() {
    dtOpstart = $('#dtOpstart').datebox('getValue');
    dtOpend = $('#dtOpend').datebox('getValue');
    var ruleArr = [];
    if (cid != "") {
        ruleArr.push({ "field": "devid", "op": "eq", "data": cid });
    }
    if (dtOpstart != '')
        ruleArr.push({ "field": "ptime", "op": "ge", "data": dtOpstart });
    if (dtOpend != '')
        ruleArr.push({ "field": "ptime", "op": "le", "data": dtOpend });
    if (ruleArr.length > 0) {
        filterObj = { groupOp: 'AND', rules: ruleArr };
        $('body').data('where', JSON.stringify(filterObj));
        $.ajax({
            type: "post",
            dataType: "json",
            url: actionURL + "?action=detail&rows=" + pSize + "&page=1&devID=" + cid + "&filter=" + JSON.stringify(filterObj),
            success: function aa(data) {
                count = data.total;
                renderinfopic(data);
                $('#pp').pagination({
                    pageSize: pSize,
                    total: count,
                    pageNumber: 1,
                    pageList: [3],
                    onSelectPage: function (pageNumber, pageSize) {
                        $.ajax({
                            type: "post",
                            dataType: "json",
                            url: actionURL + "?action=detail&rows=" + pageSize + "&page=" + pageNumber + "&devID=" + cid + "&filter=" + JSON.stringify(filterObj),
                            success: function aa(data) {
                                renderinfopic(data);
                            }
                        });
                    }
                });
            }
        });
    }
}
var CRUD = {
    show: function () {
        if (cid == "") {
            msg.error('对不起，请选择设备！');
            return;
        }

        hDialog = $("#wdialog").dialog({
            title: '发送信息', width: 380, height: 360,
            iconCls: '',
            onOpen: function () {
                $("#cc").combobox({
                    url: '/status_pic/ashx/pic.ashx?Action=getUserRoleName',
                    valueField: 'KeyId',
                    textField: 'RoleName'
                });
                $("#cc").combobox({
                    onLoadSuccess: function (data) {
                        var selectData = $("#cc").combobox('getData');
                        if (selectData != null) {
                            $("#cc ").combobox('select', selectData[0].KeyId);
                        }

                    },
                    onSelect: function () {
                        $('#cct').combobox({
                            url: '/status_pic/ashx/pic.ashx?Action=getUserInfo&roleId='
                                + $('#cc').combobox('getValue'),
                            valueField: 'UserID',
                            textField: 'TrueName',
                            multiple: "true"
                        });

                    }
                });

            },
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: sendmsg
            }, {
                text: '关闭',
                iconCls: 'icon-cancel',
                handler: function () {
                    hDialog.dialog('close');
                }
            }]
        });
    }
};

function sendmsg() {

    var userids = $("#cct").combotree('getValues');
    var content = $("#sendcontent").val();
    if (userids == "") {
        msg.error("请选择客服人员！");
        return;
    }
    $('#sendfrm').form('submit', {
        url: '/status_pic/ashx/PIC.ashx',
        type: 'POST',
        onSubmit: function (param) {
            var isValid = $('#sendfrm').form('validate');
            if (isValid) {
                param.Action = "add";//这里一定要写上参数，否则找不到方法add
                param.uinfo = userids;
                param.cinfo = content;
                param.senduid = uid;
                msg.ok('正在发送中...');
                return true;
            }
            else {
                return false;
            }
        },
        success: function (data) {
            var d = eval('(' + data + ')');
            if (d.Success) {
                hDialog.dialog('close');
                msg.ok('发送成功');
            }
            else {
                msg.error("发送失败");
            }
        }
    });



}

